/*
* 报表数据解析引擎
* 每一个图表需要的参数，都单独处理，没有做统一逻辑封装
* 因为目前就两个页面，三种图形，加之需求老是变更，单个图形处理感觉更方便
* */
import { deepFullCopy, messages, formatNumber } from "share/common";
import { _ts, lengthStrShow } from "containers/data-center/report/report-utils";

export default {
  /*
  * 饼状图
  * 报销状态占比
  * */
  statusAmountReportExample: {
    title: {
      text: '',
      left: 'left',
      textStyle: {
        fontWeight: 400
      }
    },
    tooltip: {
      trigger: 'item',
      formatter: "{b}: {c} ({d}%)"
    },
    legend: {
      bottom: 10,
      left: 'center',
      data: [ 'test' ]
    },
    series: [
      {
        type: 'pie',
        radius: [ '40%', '60%' ],
        data: [
          { value: 335, name: 'test' },
        ]
      }
    ]
  },
  /*
  * 柱状图
  * 费用类型占比
  * 部门费用占比 共用这个数据
  * 人员费用占比 共用这个数据
  * */
  expenseTypeReportExample: {
    title: {
      text: '',//初始化数据时设置
      left: 'left',
      textStyle: {
        fontWeight: 400
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    axisLabel: {
      interval: 119, // 自定义显示X轴坐标显示间隔
      textStyle: {
        color: '#626262',
        fontSize: '10',
      },
    },
    grid: {
      y: 20,
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    yAxis: [
      {
        data: [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ],
      }
    ],
    xAxis: [
      {
        type: 'value',
        axisLabel: {
          interval: 0,
          rotate: 45,
          margin: 10,
          textStyle: {
            fontWeight: "bolder",
            color: "#000000"
          }
        },
      }
    ],
    series: [
      {
        type: 'bar',
        barWidth: 24,
        data: [ 10, 52, 200, 334, 390, 330, 220 ]
      }
    ]
  },
  expenseTendencyReportExample: {
    title: {
      text: '',//初始化数据时设置
      left: 'left',
      textStyle: {
        fontWeight: 400
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    toolbox: {
      feature: {
        dataView: { show: true, readOnly: false },
        magicType: { show: true, type: [ 'line', 'bar' ] },
        restore: { show: true },
        saveAsImage: { show: true }
      }
    },
    legend: {
      data: [ messages('data-center.key58')/*当月金额*/ ],
      bottom: 10,
      left: 'center',
    },
    xAxis: [
      {
        type: 'category',
        splitLine: {
          show: true,
          lineStyle: {
            color: '#999999',
          }
        },
        data: [ '1月' ],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: messages('common.amount')/*金额*/,
        axisLabel: {
          formatter: '{value} 元'
        }
      },
    ],
    series: [
      {
        name: messages('data-center.key58')/*当月金额*/,
        type: 'bar',
        barWidth: 28,
        data: [ 21.0 ],
        //配置样式
        itemStyle: null,
      },
    ]
  },
  //费用总体趋势报表
  expenseTendencyReport(data) {
    let options = deepFullCopy(this.expenseTendencyReportExample);
    options.title._text = messages('data-center.key68');
    /*费用总体趋势报表*/
    let xAxisData = [], baseAmountArr = [];
    data.forEach(item => {
      item.value = item.baseAmount ? item.baseAmount : 0;
      xAxisData.push(item.monthName);
      baseAmountArr.push(item);
    });
    options.xAxis[0].data = xAxisData;
    options.series[0].data = baseAmountArr;
    return options;
  },
  //费用类型分析
  expenseTypeReport(data) {
    let options = deepFullCopy(this.expenseTypeReportExample);
    options.title._text = messages('data-center.key86');
    /*费用类型分析*/
    let yAxisData = [], seriesData = [];
    data.map(item => {
      yAxisData.push(_ts(item.label));
      seriesData.push(item);
    });
    options.yAxis[0].data = yAxisData;
    options.series[0].data = seriesData;
    return options;
  },
  // 报销状态占比
  statusAmountReport(data) {
    let options = deepFullCopy(this.statusAmountReportExample);
    options.title._text = messages('data-center.key63');
    let legendData = [], seriesData = [];
    data.map(item => {
      legendData.push(item.label);
      let d = {
        value: item.value,
        name: item.label,
        criteria: item.criteria
      };
      seriesData.push(d);
    });
    options.legend.data = legendData;
    options.series[0].data = seriesData;
    //提示框不截取
    options.tooltip.formatter = function (datas) {
      return lengthStrShow(datas.name) + ":</br>" + formatNumber(datas.value);
    };
    return options;
  },
  /*
  * 设置饼状图柱子的颜色
  * @params indexActive 点击柱形图序号
  * @params itemLength 柱形图数量
  * */
  getBarOptionsColors(indexActive, itemLength) {
    let colorList = [];
    for (let i = 0; i < itemLength; i++) {
      colorList.push('#ddd')
    }
    colorList[indexActive] = '#00386b';
    return colorList;
  },
  /*
   * 设置柱形图柱子的颜色
   * @params indexActive 点击柱形图序号
   * @params itemLength 柱形图数量
   * */
  getActiveItemStyle(indexActive, itemLength) {
    return {
      //通常情况下：
      normal: {
        //每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
        color: function (params) {
          let colorList = [];
          for (let i = 0; i < itemLength; i++) {
            colorList.push('#ddd')
          }
          if (indexActive >= 0) {
            colorList[indexActive] = '#00386b';
          }
          return colorList[params.dataIndex];
        }
      },

    }
  },
  /*
 * 设置柱形图柱子的颜色
 * @params params 被点击过的数据
 * @params options 当前画布被渲染的参数
 * */
  getActiveItemStyleByName(params, options) {
    let itemLength = options.series[0].data.length;
    let indexActive = getIndexActive(params, options);
    return {
      //通常情况下：
      normal: {
        //每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
        color: function (params) {
          let colorList = [];
          for (let i = 0; i < itemLength; i++) {
            colorList.push('#ddd')
          }
          if (indexActive >= 0) {
            colorList[indexActive] = '#00386b';
          }
          return colorList[params.dataIndex];
        }
      },
    };

    /*
    *
    * 获取当前被渲染的数据中，被激活元素的序号
    * @params params 被点击过的数据
    * @params options 当前画布被渲染的参数
    * @return 返回序号，没有则返回 -1
    * */
    function getIndexActive(params, options) {
      let criteria = params.data.criteria;
      let indexActive = -1;
      options.series[0].data.map((item, index) => {
        if (item.criteria === criteria) {
          indexActive = index
        }
      });
      return indexActive
    }
  },
}
